<template>
  <div class="pop-dialog-container">
    <div class="pop-dialog-main">
      <el-form label-position="right" :model="buildingForm" :label-width="formLabelWidth" ref="buildingFormRef" :rules="rules">
        <el-row>
          <el-col :span="11">
            <el-form-item label="楼栋名称" prop="buildingName">
              <el-input v-model="buildingForm.buildingName" placeholder="请输入楼栋名称" maxlength="100"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="11">
            <el-form-item label="地址" prop="address">
              <el-input v-model="buildingForm.address" placeholder="请输入地址" maxlength="255"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="11">
            <el-form-item label="楼层层数" prop="floorCount">
              <el-input-number v-model="buildingForm.floorCount" :min="1" :max="1"></el-input-number>
            </el-form-item>
          </el-col>
          <el-col :span="11">
            <el-form-item label="楼栋户数" prop="roomCount">
              <el-input-number v-model="buildingForm.roomCount" :min="1" :max="1"></el-input-number>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </div>
    <div class="pop-dialog-footer">
      <el-row>
        <el-col :span="11" :offset="6">
          <div class="form-button-container">
            <el-button type="primary" @click="onSave">保存</el-button>
            <el-button @click="onReset">重置</el-button>
            <el-button type="primary" @click="onBack">关闭</el-button>
          </div>
        </el-col>
      </el-row>
    </div>
  </div>
</template>

<script setup lang="ts">
import {defineEmits, defineProps, inject, onMounted, reactive, ref, toRefs} from 'vue';
import buildingApi from '@/api/buildingApi'
import type {FormInstance} from 'element-plus';
import {ElMessage} from 'element-plus'
import type {BuildingForm} from "@/types/req/buildingForm";
import type {Result} from '@/types/result'
import type {Building} from "@/types/resp/building";
import type {Id} from "@/types/id";

const props = defineProps<Id>()
const buildingFormRef = ref<FormInstance | null>(null);
const buildingForm = reactive<BuildingForm>({
  buildingName: '',
  address: '',
  floorCount: 0,
  roomCount: 0,
})
const emit = defineEmits<{
  (e: 'resetBuildingAddEvent'): void;
  (e: 'closeBuildingAddEvent', data?: { search: boolean }): void;
}>()

const rules = reactive({
  buildingName: [
    { required: true, message: '请输入楼栋名称', trigger: 'blur' }
  ],
});
const dialogTop = ref<string>('')
const dialogWidth = ref<string>('')
const formLabelWidth = ref<string>('')

onMounted(() => {
  if (props.id) {
    initBuildingFormById(props.id)
  }
  const globalState = inject('globalState', {} as { dialogWidth?: string, dialogTop?: string, formLabelWidth?: string })
  if (!globalState) {
    throw new Error('globalState is not provided');
  }
  const globalStateRefs = toRefs(globalState)
  if (globalStateRefs.formLabelWidth) {
    formLabelWidth.value = globalStateRefs.formLabelWidth.value || ''
  }
  if (globalStateRefs.dialogWidth) {
    dialogWidth.value = globalStateRefs.dialogWidth.value || ''
  }
  if (globalStateRefs.dialogTop) {
    dialogTop.value = globalStateRefs.dialogTop.value || ''
  }
});

const initBuildingFormById = async (id: number) => {
  const resp: Result<Building> = await buildingApi.findById(id)
  if (resp && resp.code === 1 && resp.data) {
    Object.assign(buildingForm, resp.data)
  }
}

const onReset = () => {
  if (buildingFormRef.value) {
    buildingFormRef.value.resetFields()
  }
  emit('resetBuildingAddEvent')
}

const onSave = () => {
  if (!buildingFormRef.value) {
    return
  }
  buildingFormRef.value.validate(async (valid: boolean) => {
    if (valid) {
      const resp: Result<void> = await buildingApi.save(buildingForm)
      if (resp && resp.code === 1) {
        ElMessage.success('保存成功')
        emit('closeBuildingAddEvent', {search: true})
      } else {
        ElMessage.error('保存失败:' + (resp.msg ? resp.msg : ''))
      }
    }
  })
}

const onBack = () => {
  emit('closeBuildingAddEvent')
}
</script>

<style lang="scss">
</style>
